<template>
  <div class="app-container">
    <div class="div-sch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="题目" prop="question">
          <el-input
            v-model="queryParams.question"
            placeholder="请输入题目"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="测评批次" prop="testName">
          <el-input
            v-model="queryParams.testName"
            placeholder="请输入测评批次名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="div-t">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:config:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:config:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:config:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:config:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        border
        :data="configList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="题目" :show-overflow-tooltip="true" align="center" prop="question" />
        <el-table-column
          label="题目类型"
          align="center"
          prop="questionType"
          width="100"
          :formatter="quTypeFormat"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="答案选项" align="center" prop="options" :show-overflow-tooltip="true" />
        <el-table-column
          label="测评批次名称"
          align="center"
          prop="testName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          width="180"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:config:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:config:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改考试配置 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="知识领域">
          <el-select v-model="form.domainId" :style="{width: '100%'}" placeholder="请选择知识领域">
            <el-option
              v-for="dict in domain"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="难度">
          <el-select v-model="form.level" :style="{width: '100%'}" placeholder="请选择难度">
            <el-option
              v-for="dict in quLevel"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目类型">
          <el-select v-model="form.questionType" :style="{width: '100%'}" placeholder="请选择题目类型">
            <el-option
              v-for="dict in quType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="测评批次">
          <el-select v-model="form.testName" :style="{width: '100%'}" placeholder="请选择测评批次">
            <el-option
              v-for="dict in examConfig"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目" prop="question">
          <el-input v-model="form.question" placeholder="题目" />
        </el-form-item>
        <el-form-item label="答案选项" prop="options">
          <el-input v-model="form.options" placeholder="请输入答案选项" />
        </el-form-item>
        <el-form-item label="答案" prop="answer">
          <el-input v-model="form.answer" placeholder="请输入答案" />
        </el-form-item>
        <el-form-item label="答题时间(秒)" prop="answerTime">
          <el-input v-model="form.answerTime" placeholder="请输入答题时间" />
        </el-form-item>

        <el-form-item>
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuestion,
  getQuestion,
  delQuestion,
  addQuestion,
  updateQuestion,
  listDictDomain,
  listDictLevel,
  listDictQuType,
  listDictExamConfig
} from "@/api/business/exam";

export default {
  name: "Config",
  dicts: ["sys_yes_no"],
  data() {
    return {
      // 遮罩层
      textarea: "",
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      domain: [],
      quLevel: [],
      quType: [],
      examConfig: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        testName: undefined,
        category: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "测评名称不能为空", trigger: "blur" }
        ],
        testName: [
          { required: true, message: "测评批次名称不能为空", trigger: "blur" }
        ],
        category: [{ required: true, message: "类别不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
    this.getDomain();
    this.getLevel();
    this.getQuType();
    this.getExamConfig();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listQuestion(this.addDateRange(this.queryParams, this.dateRange))
        .then(response => {
          this.configList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
        });
    },
    /** 查询领域字典 */
    getDomain() {
      this.loading = true;
      listDictDomain()
        .then(response => {
          this.domain = response.data;
        })
        .catch(err => {
          this.loading = false;
        });
    },
    /** 查询难度字典 */
    getLevel() {
      this.loading = true;
      listDictLevel()
        .then(response => {
          this.quLevel = response.data;
        })
        .catch(err => {
          this.loading = false;
        });
    },
    /** 查询题目类型 */
    getQuType() {
      this.loading = true;
      listDictQuType()
        .then(response => {
          this.quType = response.data;
        })
        .catch(err => {
          this.loading = false;
        });
    },
    /** 查询测评批次 */
    getExamConfig() {
      this.loading = true;
      listDictExamConfig()
        .then(response => {
          this.examConfig = response.data;
        })
        .catch(err => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        domainId: undefined,
        level: undefined,
        quType: undefined,
        question: undefined,
        options: undefined,
        answer: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测评信息";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.id || this.ids;
      getQuestion(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测评信息";
        this.textarea = response.data.content;
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除参数编号为"' + configIds + '"的数据项？')
        .then(function() {
          return delQuestion(configIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/config/export",
        {
          ...this.queryParams
        },
        `config_${new Date().getTime()}.xlsx`
      );
    },
    quTypeFormat(data) {
      if (data.questionType == "option") {
        return "单选题";
      } else {
        return "多选题";
      }
    }
  }
};
</script>
